package aa.jx.order.service.impl;

import aa.jx.order.mapper.OrderMapper;
import aa.jx.order.mapper.PaymentMapper;
import aa.jx.order.model.Order;
import aa.jx.order.model.PaymentInfo;
import aa.jx.order.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    @Transactional
    public PaymentInfo createPayment(Long orderId) {
        // 验证订单是否存在且状态为待支付
        Order order = orderMapper.selectById(orderId);
        if (order == null) {
            throw new RuntimeException("订单不存在");
        }
        if (order.getStatus() != 0) {
            throw new RuntimeException("订单状态异常，无法支付");
        }

        // 检查是否已存在支付记录
        PaymentInfo existingPayment = paymentMapper.selectByOrderId(orderId);
        if (existingPayment != null) {
            // 如果已有支付记录且未支付，则返回现有记录
            if (existingPayment.getStatus() == 0) {
                return existingPayment;
            }
            throw new RuntimeException("订单已支付，请勿重复支付");
        }

        // 创建新的支付记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderId(orderId);
        paymentInfo.setOrderNo(order.getOrderNo());
        paymentInfo.setPaymentAmount(order.getPaymentAmount());
        paymentInfo.setStatus(0); // 未支付状态
        paymentInfo.setPaymentTime(new Date());

        paymentMapper.insert(paymentInfo);
        return paymentInfo;
    }

    @Override
    @Transactional
    public void processPayment(Long paymentId, boolean success) {
        PaymentInfo paymentInfo = paymentMapper.selectById(paymentId);
        if (paymentInfo == null) {
            throw new RuntimeException("支付记录不存在");
        }

        // 检查支付记录状态
        if (paymentInfo.getStatus() != 0) {
            throw new RuntimeException("支付记录状态异常");
        }

        // 获取关联订单
        Order order = orderMapper.selectById(paymentInfo.getOrderId());
        if (order == null) {
            throw new RuntimeException("关联订单不存在");
        }

        if (success) {
            // 支付成功处理
            paymentInfo.setStatus(1); // 支付成功
            paymentInfo.setPaymentTime(new Date());

            // 更新订单状态为已支付
            order.setStatus(1); // 已支付状态
            order.setPaymentTime(new Date());

            paymentMapper.updateStatus(paymentInfo);
            orderMapper.updatePaymentStatus(order);
        } else {
            // 支付失败处理
            paymentInfo.setStatus(2); // 支付失败
            paymentMapper.updateStatus(paymentInfo);
        }
    }

    @Override
    public PaymentInfo getPaymentInfo(Long paymentId) {
        return paymentMapper.selectById(paymentId);
    }

    @Override
    public PaymentInfo getPaymentByOrderId(Long orderId) {
        return paymentMapper.selectByOrderId(orderId);
    }

    @Override
    @Transactional
    public void cancelPayment(Long paymentId) {
        PaymentInfo paymentInfo = paymentMapper.selectById(paymentId);
        if (paymentInfo == null) {
            throw new RuntimeException("支付记录不存在");
        }

        // 只有未支付的记录可以取消
        if (paymentInfo.getStatus() != 0) {
            throw new RuntimeException("只有未支付的记录可以取消");
        }

        // 更新支付记录状态为取消
        paymentInfo.setStatus(3); // 3表示已取消
        paymentMapper.updateStatus(paymentInfo);
    }
}